package com.winshare.saleAssistant.controller;

import com.winshare.saleAssistant.domain.dto.req.*;
import com.winshare.saleAssistant.domain.entity.StaffDO;
import com.winshare.saleAssistant.service.StaffService;
import com.winshare.saleAssistant.util.LogicResponse;
import com.winshare.saleAssistant.util.RequestHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/sys")
@CrossOrigin
@Api(value = "登录控制类", description = "登录控制类")
public class LoginController {

    private final StaffService staffService;

    public LoginController(StaffService staffService) {
        this.staffService = staffService;
    }

    @PostMapping("/login")
    @ResponseBody
    @ApiOperation(value = "登录接口,可根据此登录接口获取token",notes = "登录接口")
    public LogicResponse login(@RequestBody @Validated LoginDTO loginDTO,
                               HttpServletRequest request, HttpSession session) {
        return LogicResponse.success(staffService.login(loginDTO,request,session));
    }

    @PostMapping("/getCaptchaImage")
    @ApiOperation(value = "获取短信验证码",notes = "获取短信验证码")
    public LogicResponse getVerificationCode(@RequestBody @Validated CaptchaReqDTO captchaReqDTO) {
        staffService.getVerificationCode(captchaReqDTO);
        return LogicResponse.success("短信已成功发送，请注意查收");
    }

    @PostMapping("/login/phone")
    @ApiOperation(value = "用手机加短信验证码的方式登录/用openId的方式登录，可根据此登录接口获取token",notes = "用手机加短信验证码的方式登录")
    public LogicResponse loginByPhone(@RequestBody @Validated LoginByPhoneDTO loginByPhoneDTO,
                                      HttpServletRequest request) {
        return LogicResponse.success(staffService.loginByPhone(loginByPhoneDTO,request));
    }

    @PostMapping("/login/openId")
    @ApiOperation(value = "用openId的方式自动登录",notes = "用openId的方式自动登录")
    public LogicResponse loginByOpenId(@RequestBody @Validated LoginByOpenIdDTO loginByOpenIdDTO,
                                      HttpServletRequest request) {
        return LogicResponse.success(staffService.loginByOpenId(loginByOpenIdDTO,request));
    }

    @PostMapping("/menuListByCurrentPerson")
    @ApiOperation(value = "获取当前用户具有权限的菜单(列表形式)",notes = "获取当前用户具有权限的菜单(列表形式)")
    public LogicResponse getMenuListByCurrentPerson(HttpServletRequest request) {
        return LogicResponse.success(staffService.getMenuListByCurrent(request));
    }

    @PostMapping("/menuListByCurrentPerson/V2")
    @ApiOperation(value = "获取当前用户具有权限的菜单(树状结构)",notes = "获取当前用户具有权限的菜单(树状结构)")
    public LogicResponse getMenuListByCurrentPersonV2(HttpServletRequest request) {
        return LogicResponse.success(staffService.getMenuListByCurrentV2(request));
    }

    @PostMapping("/menuListByCurrentPersonAndApp")
    @ApiOperation(value = "获取当前用户在当前应用下面具有权限的菜单(列表形式)",notes = "获取当前用户当前应用下面具有权限的菜单(列表形式)")
    public LogicResponse getMenuListByCurrentPersonAndApp(@RequestBody @Validated BaseIdDTO baseIdDTO) {
        return LogicResponse.success(staffService.getMenuListByCurrentPersonAndApp(baseIdDTO));
    }

    @PostMapping("/menuListByCurrentPersonAndApp/V2")
    @ApiOperation(value = "获取当前用户在当前应用下面具有权限的菜单(树状结构)",notes = "获取当前用户当前应用下面具有权限的菜单(树状结构)")
    public LogicResponse getMenuListByCurrentPersonAndAppV2(@RequestBody @Validated BaseIdDTO baseIdDTO) {
        return LogicResponse.success(staffService.getMenuListByCurrentPersonAndAppV2(baseIdDTO));
    }

    @PostMapping("/appListByCurrentPerson")
    @ApiOperation(value = "获取当前用户具有权限的应用",notes = "获取当前用户具有权限的应用")
    public LogicResponse getAppListByCurrentPerson(HttpServletRequest request) {
        return LogicResponse.success(staffService.getAppListByCurrent(request));
    }

    @PostMapping("/loginOut")
    @ApiOperation(value = "退出登录",notes = "退出登录")
    public LogicResponse loginOut(HttpServletRequest request) {
        staffService.loginOut(request);
        return LogicResponse.success("success");
    }

    @PostMapping(value = "/getCurrentStaffInfo")
    @ApiOperation(value = "获取当前用戶信息",notes = "获取当前用戶信息")
    public LogicResponse getCurrentStaffInfo(){
        return LogicResponse.success(staffService.getCurrentStaffInfo());
    }

}
